<template>
  <el-form
    :model="reportTrafficFormUser"
    label-width="80px"
    :rules="rulesUser"
    ref="formRef"
  >
    <el-form-item label="事件类型" prop="type">
      <el-select v-model="reportTrafficFormUser.type">
        <el-option label="碰撞" value="碰撞"></el-option>
        <el-option label="刮擦" value="刮擦"></el-option>
        <el-option label="碾压" value="碾压"></el-option>
        <el-option label="翻车" value="翻车"></el-option>
        <el-option label="失火" value="失火"></el-option>
        <el-option label="其他" value="其他"></el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="事件地址" prop="address">
      <el-input v-model="reportTrafficFormUser.address"></el-input>
    </el-form-item>
    <el-form-item label="建筑标识">
      <el-input v-model="reportTrafficFormUser.sign"></el-input>
    </el-form-item>
    <el-form-item label="发生时间" prop="time">
      <el-date-picker
        v-model="reportTrafficFormUser.time"
        type="datetime"
        placeholder="选择日期时间"
        format="YYYY-MM-DD HH:mm:ss"
        value-format="YYYY-MM-DD HH:mm:ss"
        style="width: 100%"
      ></el-date-picker>
    </el-form-item>
    <el-form-item label="事件描述" prop="description">
      <el-input v-model="reportTrafficFormUser.description"></el-input>
    </el-form-item>
  </el-form>
  <div class="button-group">
    <el-button @click="closeForm">取消</el-button>
    <el-button type="primary" @click="submitForm">确定</el-button>
  </div>
</template>

<script setup>
import { ref } from 'vue';
import { addTrafficReport } from '../apis/index.js';
import { ElMessage } from 'element-plus';
import { storeToRefs } from 'pinia';
const emit = defineEmits(['close-form']);
import { userData } from '../stores/userData.js';
const { user } = storeToRefs(userData());
const props = defineProps({
  coordinates: {
    type: String,
  },
});
const formRef = ref(null);
//这是用户的报告路况表单
const reportTrafficFormUser = ref({
  username: user.value.username,
  type: '',
  address: '',
  sign: '',
  time: '',
  description: '',
  coordinates: null,
  status: 0,
});
//这是用户报告路况的表单验证
const rulesUser = ref({
  type: [{ required: true, message: '请选择事件类型', trigger: 'change' }],
  address: [{ required: true, message: '请输入事件地址', trigger: 'blur' }],
  time: [{ required: true, message: '请选择发生时间', trigger: 'change' }],
  description: [{ required: true, message: '请输入事件描述', trigger: 'blur' }],
});
const closeForm = () => {
  emit('close-form');
};

const submitForm = () => {
  formRef.value.validate((valid) => {
    if (valid) {
      // 表单验证通过，可以执行提交操作
      reportTrafficFormUser.value.coordinates = props.coordinates;
      addTrafficReport(reportTrafficFormUser.value)
        .then((res) => {
          ElMessage.success('提交成功');
          emit('close-form');
        })
        .catch((err) => {
          ElMessage.error('提交失败');
        });
    } else {
      ElMessage.error('请填写完整信息');
      return false;
    }
  });
};
</script>

<style scoped>
.button-group {
  display: flex;
  justify-content: flex-end;
  margin-top: 30px;
}
</style>
